package com.jinbo.footmessage.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;

import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.CommentBean;
import com.jinbo.footmessage.httpbean.OrderBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.utils.TimeUtils;
import com.jinbo.footmessage.view.TitleBarView;

public class CommentActivity extends BaseActivity implements View.OnClickListener{

    private static String ID_INTENT = "id_bean";
    public static void launch(Activity activity,int order_id,int request_code) {
        Intent intent = new Intent(activity, CommentActivity.class);
        intent.putExtra(ID_INTENT,order_id);
        activity.startActivityForResult(intent,request_code);
    }
    RatingBar mStarRt;
    EditText mCommentEt;
    Button mSubmitBt;
    int order_id;
    float star_rating=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("评论");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        mStarRt=(RatingBar)findViewById(R.id.rating);
        mStarRt.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                star_rating=rating;
            }
        });
        mCommentEt=(EditText)findViewById(R.id.comment);
        mSubmitBt=(Button)findViewById(R.id.submit);
        mSubmitBt.setOnClickListener(this);
        Intent intent = this.getIntent();
        order_id=intent.getIntExtra(ID_INTENT, -1);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.submit:
                if(mCommentEt.getText().toString().trim().length()>0)
                {
                    BusinessController.getInstance().commentOrder(order_id,mCommentEt.getText().toString().trim(),
                            star_rating, TimeUtils.getNowDateFormat(), new Listener<CommentBean>() {
                                @Override
                                public void onStart(Object... params) {
                                    showProgressDialog("请稍后...");
                                    //mLoadView.setVisibility(View.VISIBLE);
                                    //mPullRefreshLayout.setRefreshing(true);

                                }

                                @Override
                                public void onComplete(CommentBean result, Object... params) {
                                    dismissProgressDialog();
                                    show("提交评价成功");
                                    Intent intent = new Intent();
                                    intent.putExtra("star", star_rating);
                                    intent.putExtra("comment",mCommentEt.getText().toString().trim() );
                                    setResult(RESULT_OK, intent);
                                    finish();


                                }

                                @Override
                                public void onFail(final String msg, Object... params) {
                                    dismissProgressDialog();
                                    //mLoadView.setVisibility(View.GONE);
                                    // mPullRefreshLayout.setRefreshing(false);
                                    if (TextUtils.isEmpty(msg)) {
                                    } else {
                                        show(msg);
                                    }
                                }
                            });
                }
                else
                {
                    show("请输入评价内容");
                }
                break;
        }
    }
}

